<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto">
			<el-card shadow="hover" class="pageQueryBoxArea mb10">
				<el-form :inline="true">
					<el-form-item label="代理类型：">
						<el-select v-model="state.tableData.param.agentLevelId" clearable placeholder="全部类型"
							@change="getTableData()">
							<el-option v-for="item in state.agentLevelIdList" :key="item.agentLevelId" :label="item.agentName"
								:value="item.agentLevelId" />
						</el-select>
					</el-form-item>
					<el-form-item label="代理状态：">
						<el-select v-model="state.tableData.param.agentStatus" clearable placeholder="全部状态"
							@change="getTableData()">
							<el-option v-for="item in state.agentStatusList" :key="item.status" :label="item.label"
								:value="item.status" />
						</el-select>
					</el-form-item>
					<el-form-item label="选择时间">
						<el-date-picker v-model="state.tableData.param.time" placeholder="请选择时间" type="month"
							@change="getTableData()" format="YYYY/MM" value-format="YYYY-MM" :disabled-date="disabledDate" />
					</el-form-item>
					<el-form-item label="代理信息:">
						<el-input v-model="state.tableData.param.keyWords" placeholder="姓名、手机、微信昵称搜索" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="getTableData()">
							<el-icon>
								<ele-Search />
							</el-icon> 搜 索
						</el-button>
					</el-form-item>
				</el-form>
			</el-card>
			<el-card shadow="hover" style="display: flex;flex-direction: column;flex: 1;height: inherit;">
				<section style="display: flex;flex-direction: column; flex: 1;height: inherit;">
					<el-table :data="state.tableData.data" v-loading="state.tableData.loading">
						<el-table-column prop="agentName" label="姓名" show-overflow-tooltip></el-table-column>
						<el-table-column prop="agentPhone" label="手机号" show-overflow-tooltip></el-table-column>
						<el-table-column label="代理类型" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ scope.row.agentLevelName }}</span>

							</template>
						</el-table-column>
						<el-table-column label="本月业绩" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ scope.row.agentPerformanceMonth }}</span>
							</template>
						</el-table-column>
						<el-table-column label="总业绩" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ scope.row.agentPerformanceTotal }}</span>
							</template>
						</el-table-column>
						<el-table-column label="代理状态" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ scope.row.agentStatus == 1 ? '开启' : '关闭' }}</span>

							</template>
						</el-table-column>
						<el-table-column label="时间" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ formatDate(new Date(scope.row.updateTime), 'YYYY-mm') }}</span>

							</template>
						</el-table-column>
						<el-table-column label="操作" width="100">
							<template #default="scope">
								<el-link class="font16" @click="onLookPerformance(scope.row)" :underline="false"
									type="primary">明细</el-link>
								<span style="color:var(--el-color-primary);">&nbsp;|&nbsp;</span>
								<el-link class="font16" :underline="false" type="primary">
									<el-dropdown>
										<span style="color:var(--el-color-primary);" class="pointer font16"> 更多 </span>
										<template #dropdown>
											<el-dropdown-menu>
												<el-dropdown-item @click="onEdit('edit', scope.row)">调整业绩</el-dropdown-item>
												<el-dropdown-item @click="onLookHistory(scope.row)"> 历史业绩</el-dropdown-item>

											</el-dropdown-menu>
										</template>
									</el-dropdown>
								</el-link>
							</template>
						</el-table-column>
					</el-table>
					<el-row justify="end">
						<el-col :span="24" class="flex-end-center">
							<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
								:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum"
								background v-model:page-size="state.tableData.param.pageSize"
								layout="total, sizes, prev, pager, next, jumper" :total="state.tableData.total">
							</el-pagination>
						</el-col>
					</el-row>
				</section>
			</el-card>
		</div>
		<adjustPerformanceDialog ref="adjustPerformanceRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemRole">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { getAgentPerformanceList, agentList } from "/@/api/agent";
import { formatDate } from '/@/utils/formatTime';
import { useRouter } from 'vue-router';
const router = useRouter();

// 定义变量内容
const disabledDate = (time: Date) => time.getTime() > Date.now();

const adjustPerformanceDialog = defineAsyncComponent(() => import('./compoent/adjustPerformanceDialog.vue'));



// 定义变量内容
const adjustPerformanceRef = ref();
const state = reactive<any>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			keywords: "",//姓名、手机、微信昵称搜索
			agentLevelId: "",//代理级别id
			time: "",//变更开始结束时间
			pageNum: 1,
			pageSize: 10,
			agentStatus: "" //代理状态

		},
	},
	agentLevelIdList: [],
	agentStatusList: [{
		label: "未开启",
		status: 0
	}, {
		label: "已开启",
		status: 1
	}]

});

const getAgentList = async () => {
	const { results } = await agentList();
	state.agentLevelIdList = results;
}
// 获取商品列表
const getTableData = async () => {
	state.tableData.loading = true;

	let params = <any>{
		keywords: state.tableData.param.keyWords,
		current: state.tableData.param.pageNum,
		size: state.tableData.param.pageSize,
		agentStatus: state.tableData.param.agentStatus
	}
	if (state.tableData.param.agentLevelId != "") {
		params.agentLevelId = state.tableData.param.agentLevelId
	}
	if (state.tableData.param.time != "") {
		params.time = state.tableData.param.time
	}

	const { results } = await getAgentPerformanceList(params);
	state.tableData.loading = false;
	state.tableData.data = results.records;
	state.tableData.total = results.total;
}

// 打开新增规格弹窗
const onEdit = (type: string, row?: Object) => {
	adjustPerformanceRef.value.openDialog(type, JSON.parse(JSON.stringify(row)));
};
const onLookPerformance = (row: any) => {
	router.push({
		path: '/agent/data/performanceRecord',
		query: { id: row.agentId },
	});
};//查看明细
const onLookHistory =
	(row: any) => {
		router.push({
			path: '/agent/data/historyRecord',
			query: { id: row.agentId },
		});
	};//查看历史明细
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};

// 页面加载时
onMounted(() => {
	getAgentList()
	getTableData();
});
</script>

<style scoped lang="scss">
@import '../../../theme/mixins/index.scss';

:deep(.el-card__body) {
	display: flex;
	flex-direction: column;
	flex: 1;
	height: inherit;

	.el-table {
		flex: 1;

		.el-table__inner-wrapper::before {
			background: none !important;
		}
	}
}
</style>
